﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace BEELab
{
    public partial class ResetPassword : System.Web.UI.Page
    {
        /// <summary>
        /// Occures on every page load (first time or Post Back).
        /// Check if we got "Token" random string from previous page
        /// so we can retrieve user's email from DB according to.
        /// If we don't - redirect user to password recovery page.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Page_Load(object sender, EventArgs e)
        {
            //If there id no Token
            if (Request.QueryString["Token"] == null)
            {
                Response.Redirect("PasswordRecovery.aspx");
            }
            //If there is Token but there is no matching email
            else
            {
                string Email = BEELab.Logic.UserManagement.EmailFromToken(Request.QueryString["Token"]);
                if (Email == null || Email.Length == 0)
                {
                    Response.Redirect("PasswordRecovery.aspx");
                }
            }
        }

        /// <summary>
        /// Occures when an existing user forgot his password and want to reset it.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            if (BEELab.Logic.UserManagement.ChangePassword(Request.QueryString["Token"], txtNewPassword.Text))
                Response.Redirect("UserManagement.aspx");
            else
                Response.Redirect("PasswordRecovery.aspx");
        }
    }
}